﻿#region Header

// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------

#endregion

#region Usings

using System;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.Reflection;
using ScrumTable.Common.Logging;
using ScrumTable.Common.Properties;

#endregion

namespace ScrumTable.Common.FileSystem
{
    /// <summary>
    /// Specifies an entry for the filepool. After the file pool item isn't
    /// referenced in the object tree, the underlying file will be deleted.
    /// </summary>
    public class FilePoolHandle : DisposeBase
    {
        #region Declarations

        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly string _fullFilePath;
        private readonly bool _parentDirIsComposed;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
        
        /// <summary>
        /// Gets the full file path of the managed file.
        /// </summary>
        public string FullPath
        {
            get { return _fullFilePath; }
        }

        /// <summary>
        /// Gets the uri of the managed file.
        /// </summary>
        public Uri Uri
        {
            get { return new Uri(_fullFilePath, UriKind.RelativeOrAbsolute); }
        }

        #endregion

        #region Constructors / Destructor

        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        internal FilePoolHandle(string fullFilePath, bool parentDirIsComposed)
        {
            PreCondition.AssertNotNull(fullFilePath, "fullFilePath");

            _fullFilePath = fullFilePath;
            _parentDirIsComposed = parentDirIsComposed;
        }

        #endregion

        #region Methods

        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Releases unmanaged resources.
        /// </summary>
        protected override void ReleaseUnmanagedResources()
        {
            try
            {
                FileInfo file = new FileInfo(_fullFilePath);

                if (_parentDirIsComposed && file.Directory.Exists)
                {
                    file.Directory.Delete(true);
                }
                else if (file.Exists)
                {
                    file.Delete();
                }
            }
            catch (Exception e)
            {
                Logger<FilePoolHandle>.Error(Resources.ExcFilePoolDeletedFailed, e);
            }
        }

        #endregion

        #region Events

        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}